package cn.cvs.dao;

import cn.cvs.pojo.SysUser;
import org.apache.ibatis.annotations.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Set;

//@CacheNamespace//开启注解定义的二级缓存
public interface SysUserMapper {

    //依据account和password查询用户
    @Select("select * from t_sys_user where account =#{account} and password =#{password}")
    SysUser queryOne(@Param("account") String account, @Param("password") String password);

    //查询所有用户
    List<SysUser> queryAll();

    //查询所有用户
    @Select("select * from t_sys_user")
    List<SysUser> findAll();

    //依据编号查询用户
    @Select("select u.*,r.roleName from " +
            "t_sys_user u left join t_sys_role r " +
            "on u.roleId= r.id " +
            "where u.id =#{id}")
    @ResultMap("SysUserMap")//可以直接引用到SQL映射文件中的resultMap
    SysUser queryById(int id);


    //依据编号查询用户
    SysUser findById(Integer id);

    //多条件查询 真实姓名realName(模糊查询) 角色编号(roleId)
    List<SysUser> queryList(SysUser sysUser);

    //多条件Map传参
    List<SysUser> queryListWithMap(Map map);

    //多条件 使用注解@Param
    List<SysUser> queryListWithParam(@Param("realName") String realName, @Param("roleId")Integer roleId);
    //查询所有用户(带有角色名称 userRoleName)
    List<SysUser> queryListWithRoleName();
    //查询所有用户（带有关联属性，角色对象）
    List<SysUser> queryListWithRole();
    //添加一个用户
    int addAll(SysUser sysUser);

    //修改用户
    //依据编辑（主键）去更新，能够更新除主键外所有的字段
    int modify(SysUser sysUser);
    //依据编号删除用户
    @Delete("delete from t_sys_user where id=#{id}")
    int deleteById(Integer id);




    //以下使用动态sql
    //if
    List<SysUser> queryDList(@Param("realName") String realName, @Param("roleId") Integer roleId);

    //if+where
    List<SysUser> queryDList2(@Param("realName") String realName, @Param("roleId") Integer roleId);
    //choose + when
    List<SysUser> queryDList3(@Param("account") String account, @Param("realName") String realName, @Param("roleId") Integer roleId);

    //foreach
    List<SysUser> queryForEach(List<Integer> ids);
    List<SysUser> queryForEach2(@Param("ids") Set<Integer> ids);
    List<SysUser> queryForEach2(@Param("ids") Integer[] ids);
    List<SysUser> queryForEach3(@Param("idMap") Map<String,Integer> idMap);

    //修改if + set
    int update(SysUser sysUser);
    //查询trim
    List<SysUser> queryTrimList(@Param("realName") String realName, @Param("roleId") Integer roleId);
    //修改trim
    int updateTrim(SysUser sysUser);

    //分页
    List<SysUser> queryPage(@Param("pageBean") Integer pageBean, @Param("pageSize") Integer pageSize);



    //依据realName和roleId查询 + 分页
    //使用动态SQL
    List<SysUser> queryByPage(@Param("realName") String realName, @Param("roleId") Integer roleId, @Param("pageBegin") Integer pageBegin, @Param("pageSize") Integer pageSize);
    //参与条件(realName和roleId)分页的总记录数
    int queryCount(@Param("realName") String realName, @Param("roleId") Integer roleId);

    //查询用户是否存在
    @Select("select count(0) from t_sys_user where account=#{account}")
    int queryUserExist(String account);
}
